package net.yihang.demo.demo_quartz.controller;

import net.yihang.demo.demo_quartz.jobs.Job0;
import net.yihang.demo.demo_quartz.jobs.Job1;
import net.yihang.demo.demo_quartz.jobs.Job2;
import org.quartz.*;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

import static org.quartz.CronScheduleBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;

@RestController
public class QuartzController {

    private Scheduler scheduler;

    public QuartzController(Scheduler scheduler) {
        this.scheduler = scheduler;
    }

    @GetMapping("/start/job0")
    public String job0() throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(Job0.class).withIdentity("job0").build();
        CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0/5 * * * * ? *");
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("t0").withSchedule(cron).build();
        scheduler.scheduleJob(jobDetail,trigger);
        return "job0 start";
    }

    @GetMapping("/start/job1")
    public String job1() throws SchedulerException {
        JobDetail jobDetail = scheduler.getJobDetail(new JobKey("job1", Scheduler.DEFAULT_GROUP));
        if(jobDetail == null) {
            jobDetail = JobBuilder.newJob(Job1.class).withIdentity("job1")
                    .usingJobData("name", "zhangsan")
                    .storeDurably()
                    .build();
            scheduler.addJob(jobDetail,true);
        }
        TriggerKey key1 = new TriggerKey("trigger1", Scheduler.DEFAULT_GROUP);
        Trigger trigger = scheduler.getTrigger(key1);
        if(trigger == null) {
            trigger = TriggerBuilder.newTrigger().withIdentity(key1).withSchedule(
                    cronSchedule("0/5 * * * * ? *")
            ).usingJobData("exception",false).forJob(jobDetail).build();
            scheduler.scheduleJob(trigger);
        } else {
            scheduler.rescheduleJob(key1, trigger);
        }

//        TriggerKey key2 = new TriggerKey("trigger2", Scheduler.DEFAULT_GROUP);
//        Trigger trigger2 = TriggerBuilder.newTrigger().withIdentity(key2).withSchedule(
//                cronSchedule("0/1 * * * * ? *")
//        ).usingJobData("exception",false).forJob(jobDetail).build();
//        scheduler.rescheduleJob(new TriggerKey("trigger2", Scheduler.DEFAULT_GROUP), trigger2);

        return "job1 start";
    }

    @GetMapping("/job2")
    public String job2() throws SchedulerException {
        JobDetail jd = JobBuilder.newJob(Job2.class).withIdentity("k1", "group1").storeDurably().build();

        Trigger trigger1 = TriggerBuilder.newTrigger().withIdentity("t1", "group1")
                    .withSchedule(simpleSchedule().withIntervalInSeconds(5).withRepeatCount(3)).forJob(jd).build();

        Trigger trigger2 = TriggerBuilder.newTrigger().withIdentity("t2", "group1")
                .withSchedule(simpleSchedule().withIntervalInSeconds(5).withRepeatCount(3)).forJob(jd).build();

        scheduler.addJob(jd,false);
        scheduler.scheduleJob(trigger1);
        scheduler.scheduleJob(trigger2);
        Set<JobKey> group1 = scheduler.getJobKeys(GroupMatcher.jobGroupEquals("group1"));
        System.out.println(group1);
        return "job2";

    }
}
